package com.bls.cate.ui.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/1/18.
 */

public class MDialog {


    /**
     * @param context    content
     * @param themeId    theme 没有theme时传-1
     * @param title      title
     * @param message    message
     * @param btn1       确定按钮
     * @param btn2       取消按钮
     * @param btn3       中立按钮
     * @param dialogFlag 存在多个Dialog时 用于区分dialog
     * @param listener   按钮监听事件
     */
    public static void showCommonDialog(Context context, int themeId, String title, String message, String btn1, String btn2, String btn3, final int dialogFlag, final AlertDialogUtilListener listener) {
        AlertDialog.Builder builder = -1 == themeId ? new AlertDialog.Builder(context) : new AlertDialog.Builder(context, themeId);
        if (null != title) builder.setTitle(title);
        if (null != message) builder.setMessage(message);
        if (null != btn1) builder.setPositiveButton(btn1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.MyAlertDialogListener(dialog, dialogFlag, MDialogBtnEnum.CONFIRM);
            }
        });
        if (null != btn2) builder.setNegativeButton(btn2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.MyAlertDialogListener(dialog, dialogFlag, MDialogBtnEnum.CANCEL);
            }
        });
        if (null != btn3) builder.setNeutralButton(btn3, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.MyAlertDialogListener(dialog, dialogFlag, MDialogBtnEnum.NEUTRAL);
            }
        });
        builder.show();
    }

    /**
     * theme title message 两个按钮
     */
    public static void showCommonDialog(Context context, int themeId, String title, String message, String btn1, String btn2, final int dialogFlag, final AlertDialogUtilListener listener) {
        AlertDialog.Builder builder = 0 == themeId ? new AlertDialog.Builder(context) : new AlertDialog.Builder(context, themeId);
        if (null != title) builder.setTitle(title);
        if (null != message) builder.setMessage(message);
        if (null != btn1) builder.setPositiveButton(btn1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.MyAlertDialogListener(dialog, dialogFlag, MDialogBtnEnum.CONFIRM);
            }
        });
        if (null != btn2) builder.setNegativeButton(btn2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.MyAlertDialogListener(dialog, dialogFlag, MDialogBtnEnum.CANCEL);
            }
        });
        builder.show();
    }

    /**
     * theme title message 一个按钮
     */
    public static void showCommonDialog(Context context, int themeId, String title, String message, String btn1, final int dialogFlag, final AlertDialogUtilListener listener) {
        AlertDialog.Builder builder = 0 == themeId ? new AlertDialog.Builder(context) : new AlertDialog.Builder(context, themeId);
        if (null != title) builder.setTitle(title);
        if (null != message) builder.setMessage(message);
        if (null != btn1) builder.setPositiveButton(btn1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.MyAlertDialogListener(dialog, dialogFlag, MDialogBtnEnum.CONFIRM);
            }
        });
        builder.show();
    }

    /**
     * title message 两个按钮
     */
    public static void showCommonDialog(Context context, String title, String message, String btn1, String btn2, final int dialogFlag, final AlertDialogUtilListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        if (null != title) builder.setTitle(title);
        if (null != message) builder.setMessage(message);
        if (null != btn1) builder.setPositiveButton(btn1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.MyAlertDialogListener(dialog, dialogFlag, MDialogBtnEnum.CONFIRM);
            }
        });
        if (null != btn2) builder.setNegativeButton(btn2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.MyAlertDialogListener(dialog, dialogFlag, MDialogBtnEnum.CANCEL);
            }
        });
        builder.show();
    }

    /**
     * title message 一个按钮
     */
    public static void showCommonDialog(Context context, String title, String message, String btn1, final int dialogFlag, final AlertDialogUtilListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        if (null != title) builder.setTitle(title);
        if (null != message) builder.setMessage(message);
        if (null != btn1) builder.setPositiveButton(btn1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.MyAlertDialogListener(dialog, dialogFlag, MDialogBtnEnum.CONFIRM);
            }
        });
        builder.show();
    }


    public interface AlertDialogUtilListener {
        /**
         * 按钮点击事件
         *
         * @param dialog     dialog
         * @param dialogFlag 存在多个Dialog时 用于区分dialog
         * @param btnEnum    按钮类型 1 确定 2 取消 3 中立
         */
        void MyAlertDialogListener(DialogInterface dialog, int dialogFlag, MDialogBtnEnum btnEnum);
    }

    //确定，取消，中立
    public enum MDialogBtnEnum {
        CONFIRM, CANCEL, NEUTRAL
    }
}
